/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.husong.util;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @author maxmin
 *
 * @date 2015年8月11日 上午9:31:55
 */
public class JPush {
	
	private static final String MESSAGE_TITLE = "呼送经纪人";
	
	/*****************************生产环境消息推送配置开始***************************************/
//	private static final String USER_APPKEY = "95946a76565c798849a4c737";
//	private static final String USER_MASTERSECRET = "3aa0ecd83a6492f2ef782243";
//
//	private static final String BROKER_APPKEY = "32083e47dde410e0bbc37832";
//	private static final String BROKER_MASTERSECRET = "8e52c493df211e4248a8fd60";
	/********************************生产环境消息推送配置结束***********************************/
	
	/*****************************测试环境消息推送配置开始***************************************/
//	private static final String USER_APPKEY = "950f503b00f5b61205ffe74f";
//	private static final String USER_MASTERSECRET = "124a55306b68aaf5cf5be8a8";
//
//	private static final String BROKER_APPKEY = "ee2130972d8a3f8cd7e9a964";
//	private static final String BROKER_MASTERSECRET = "03f725d3dbe44d73bb0e9ffe";
	/********************************测试环境消息推送配置结束***********************************/
	
	/***********************************************************************************/
	private String userAppkey ;
	private String userMastersecret ;
	
	private String brokerAppkey ;
	private String brokerMastersecret;
	
	private JPushClient userClient;
	private JPushClient brokerClient;
	private static JPush jpush;
	public static JPush getInstance(){
		if (jpush==null) {
			jpush = new JPush();
		}
		return jpush;
	}
	/***********************************************************************************/

	private static final Logger logger = LoggerFactory.getLogger(JPush.class.getName());

//	public static JPushClient getUserJPushClient() {
//		return new JPushClient(USER_MASTERSECRET, USER_APPKEY);
//	}
//
//	public static JPushClient getBrokerJPushClient() {
//		return new JPushClient(BROKER_MASTERSECRET, BROKER_APPKEY);
//	}

	/**
	 * 推送所有用户
	 * 
	 * @param client
	 * @param content
	 */
	public void pushAll(JPushClient client, String content) {
		try {
			Builder builder = PushPayload.newBuilder();
			builder.setPlatform(Platform.all());
			builder.setAudience(Audience.all());
			builder.setNotification(Notification
					.newBuilder()
					.addPlatformNotification(
							AndroidNotification.newBuilder().setAlert(content)
									.setBuilderId(1).setTitle(content).build())
					.addPlatformNotification(
							IosNotification.newBuilder().setAlert(content)
									.autoBadge().setContentAvailable(true)
									.setSound("default").build()).build());
			PushPayload payload = builder.build();
			PushResult result = client.sendPush(payload);
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 所有平台，推送目标是别名为 tag，通知内容为 content。
	 * 
	 * @param client
	 * @param content
	 * @param tag
	 */
	public void pushWithTag(JPushClient client, String content,
			Set<String> tags) {
		try {
			PushPayload payload = PushPayload
					.newBuilder()
					.setPlatform(Platform.all())
					.setAudience(Audience.tag(tags))
					.setNotification(
							Notification
									.newBuilder()
									.addPlatformNotification(
											AndroidNotification.newBuilder()
													.setAlert(content)
													.setBuilderId(1)
													.setTitle(content).build())
									.addPlatformNotification(
											IosNotification.newBuilder()
													.setAlert(content)
													.autoBadge()
													.setContentAvailable(true)
													.setSound("default")
													.build()).build()).build();
			PushResult result = client.sendPush(payload);
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 所有平台，推送目标是别名为 alias，通知内容为 content。
	 * 
	 * @param client
	 * @param content
	 * @param alias
	 */
	public void pushWithAlias(JPushClient client, String content,
			String alias) {
		try {
			PushPayload payload = PushPayload
					.newBuilder()
					.setPlatform(Platform.all())
					.setAudience(Audience.alias(alias))
					.setNotification(
							Notification
									.newBuilder()
									.addPlatformNotification(
											AndroidNotification.newBuilder()
													.setAlert(content)
													.setBuilderId(1)
													.setTitle(content).build())
									.addPlatformNotification(
											IosNotification.newBuilder()
													.setAlert(content)
													.autoBadge()
													.setContentAvailable(true)
													.setSound("default")
													.build()).build()).build();
			PushResult result = client.sendPush(payload);
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void pushLBSMessageAll(JPushClient client, String content,String lng,String lat) {
		try {
			PushPayload payload = PushPayload
					.newBuilder()
					.setPlatform(Platform.android_ios())
					.setAudience(Audience.all())
					.setOptions(Options.newBuilder().setTimeToLive(0l).build())
					.setMessage(
							Message.newBuilder().setMsgContent(content)
									.setTitle(MESSAGE_TITLE)
									.addExtra("lng", lng).addExtra("lat", lat)
									.build()).build();
			PushResult result = client.sendPush(payload);
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
			System.out.println("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}
	
	public void pushLBSMessageAllWithExTra(JPushClient client, String content,Map<String, String> ext) {
		try {
			Builder builder = PushPayload.newBuilder();
			builder.setPlatform(Platform.android_ios());
			builder.setAudience(Audience.all());
			builder.setOptions(Options.newBuilder().setTimeToLive(0l).build());
			cn.jpush.api.push.model.Message.Builder mBuilder = Message.newBuilder();
			mBuilder.setMsgContent(content);
			mBuilder.setTitle(MESSAGE_TITLE);
			mBuilder.addExtras(ext);
			builder.setMessage(mBuilder.build());
			PushResult result = client.sendPush(builder.build());
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
			System.out.println("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public void pushLBSMessageAlias(JPushClient client, String content,
			String alias,String lng,String lat) {
		try {
			PushPayload payload = PushPayload
					.newBuilder()
					.setPlatform(Platform.android_ios())
					.setAudience(Audience.alias(alias))
					.setOptions(Options.newBuilder().setTimeToLive(0l).build())
					.setMessage(
							Message.newBuilder().setMsgContent(content)
									.setTitle(MESSAGE_TITLE)
									.addExtra("lng", lng).addExtra("lat", lat)
									.build()).build();
			PushResult result = client.sendPush(payload);
			logger.debug("notice sended: " + new Date() + ",result: "
					+ result.toString());
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the userClient
	 */
	public JPushClient getUserClient() {
		System.out.println("userMastersecret:"+userMastersecret+",userAppkey:"+userAppkey);
		if (userClient==null) {
			userClient=new JPushClient(userMastersecret, userAppkey);
		}
		return userClient;
	}

	/**
	 * @param userClient the userClient to set
	 */
	public void setUserClient(JPushClient userClient) {
		this.userClient = userClient;
	}

	/**
	 * @return the brokerClient
	 */
	public JPushClient getBrokerClient() {
		System.out.println("brokerMastersecret:"+brokerMastersecret+",brokerAppkey:"+brokerAppkey);
		if (brokerClient==null) {
			brokerClient = new JPushClient(brokerMastersecret, brokerAppkey);
		}
		return brokerClient;
	}

	/**
	 * @param brokerClient the brokerClient to set
	 */
	public void setBrokerClient(JPushClient brokerClient) {
		this.brokerClient = brokerClient;
	}

	/**
	 * @return the userAppkey
	 */
	public String getUserAppkey() {
		return userAppkey;
	}

	/**
	 * @param userAppkey the userAppkey to set
	 */
	public void setUserAppkey(String userAppkey) {
		System.out.println("userAppkey:"+userAppkey);
		this.userAppkey = userAppkey;
	}

	/**
	 * @return the userMastersecret
	 */
	public String getUserMastersecret() {
		return userMastersecret;
	}

	/**
	 * @param userMastersecret the userMastersecret to set
	 */
	public void setUserMastersecret(String userMastersecret) {
		System.out.println("userMastersecret:"+userMastersecret);
		this.userMastersecret = userMastersecret;
	}

	/**
	 * @return the brokerAppkey
	 */
	public String getBrokerAppkey() {
		return brokerAppkey;
	}

	/**
	 * @param brokerAppkey the brokerAppkey to set
	 */
	public void setBrokerAppkey(String brokerAppkey) {
		this.brokerAppkey = brokerAppkey;
	}

	/**
	 * @return the brokerMastersecret
	 */
	public String getBrokerMastersecret() {
		return brokerMastersecret;
	}

	/**
	 * @param brokerMastersecret the brokerMastersecret to set
	 */
	public void setBrokerMastersecret(String brokerMastersecret) {
		this.brokerMastersecret = brokerMastersecret;
	}
}
